package com.zyh.appointment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zyh.appointment.domain.Appointment;
import com.zyh.appointment.domain.AppointmentVo;
import com.zyh.appointment.service.IAppointmentService;
import com.zyh.common.annotation.Log;
import com.zyh.common.core.controller.BaseController;
import com.zyh.common.core.domain.AjaxResult;
import com.zyh.common.core.page.TableDataInfo;
import com.zyh.common.enums.BusinessType;
import com.zyh.common.utils.SecurityUtils;
import com.zyh.common.utils.StringUtils;
import com.zyh.common.utils.poi.ExcelUtil;
import com.zyh.counselor.domain.Counselor;
import com.zyh.counselor.service.ICounselorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 咨询预约Controller
 *
 * @author zhangyangheng
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/appointment/appointment")
public class AppointmentController extends BaseController {
    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private ICounselorService counselorService;


    /**
     * 咨询师处理预约申请
     *
     * @param appointment
     * @return
     */
    @PutMapping("/handleApprove")
    public AjaxResult handleApprove(@RequestBody Appointment appointment) {
        logger.info("handleApprove-->{}", appointment);
        return appointmentService.handleApprove(appointment);
    }


    /**
     * 咨询师获取自己的预约列表
     *
     * @param appointment
     * @return
     */
    @GetMapping("/adminGetList")
    public TableDataInfo adminGetList(Appointment appointment) {
        logger.info("adminGetList入参-->{}", appointment);
        startPage();
        Counselor counselor = counselorService.getOne(new LambdaQueryWrapper<Counselor>()
                .eq(Counselor::getUserId, SecurityUtils.getUserId()));
        // 没有找到说明没有待处理的咨询申请
        if (Objects.isNull(counselor)) {
            List<Appointment> list = new ArrayList<>();
            return getDataTable(list);
        }
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<Appointment>()
                .eq(Appointment::getCounselorId, counselor.getCounselorId())
                .eq(Appointment::getStatus, "0")
                .eq(Appointment::getDelFlag, "0");
        if (StringUtils.isNotBlank(appointment.getType())) {
            wrapper.eq(Appointment::getType, appointment.getType());
        }
        List<Appointment> list = appointmentService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 用户获取自己的预约列表
     *
     * @param appointment
     * @return
     */
    @GetMapping("/userGetMyList")
    public TableDataInfo userGetMyList(Appointment appointment) {
        startPage();
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<Appointment>()
                .eq(Appointment::getUserId, SecurityUtils.getUserId())
                .eq(Appointment::getDelFlag, "0");
        if (StringUtils.isNotBlank(appointment.getType())) {
            wrapper.eq(Appointment::getType, appointment.getType());
        }
        List<Appointment> list = appointmentService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 预约咨询师的方法
     *
     * @param appointment
     * @return
     */
    @PostMapping("/booking")
    public AjaxResult booking(@RequestBody Appointment appointment) {
        logger.info("booking入参-->{}", appointment);
        return appointmentService.booking(appointment);
    }


    /**
     * 查询咨询预约列表
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appointment appointment) {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }

    /**
     * 导出咨询预约列表
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:export')")
    @Log(title = "咨询预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Appointment appointment) {
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<Appointment>(Appointment.class);
        util.exportExcel(response, list, "咨询预约数据");
    }

    /**
     * 获取咨询预约详细信息
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:query')")
    @GetMapping(value = "/{appointmentId}")
    public AjaxResult getInfo(@PathVariable("appointmentId") Long appointmentId) {
        logger.info("getInfo入参appointmentId-->{}", appointmentId);
        AppointmentVo vo = appointmentService.getInfo(appointmentId);
        return AjaxResult.success(vo);
    }

    /**
     * 新增咨询预约
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:add')")
    @Log(title = "咨询预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Appointment appointment) {
        return toAjax(appointmentService.insertAppointment(appointment));
    }

    /**
     * 修改咨询预约
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:edit')")
    @Log(title = "咨询预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Appointment appointment) {
        return toAjax(appointmentService.updateAppointment(appointment));
    }

    /**
     * 删除咨询预约
     */
    //@PreAuthorize("@ss.hasPermi('appointment:appointment:remove')")
    @Log(title = "咨询预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appointmentIds}")
    public AjaxResult remove(@PathVariable Long[] appointmentIds) {
        return toAjax(appointmentService.deleteAppointmentByAppointmentIds(appointmentIds));
    }
}
